package TangramCore;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;

public class Vzhled {
  private Stroke drawStroke;
  private Color  drawColor [];
  private Paint  fillPaint [];

  public double add;
  public static Stroke tlustyStroke = new BasicStroke(0.2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
  public static Stroke tenkyStroke = new BasicStroke(0.05f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

//  private Paint  clearPaint;

  public Vzhled(Stroke ds, Color dp[], Paint fp[]) {
    drawStroke = ds;
    if(ds == null)
      add = 0;
    else{
      Rectangle2D r = new Rectangle2D.Double(0,0,1,1);
      Shape s = drawStroke.createStrokedShape(r);
      Rectangle2D r2 = s.getBounds2D();
      add = -r2.getX();
    }
    drawColor = dp;
    fillPaint = fp;
  }

  public void plotShape(Graphics2D g, Shape a) {
    //dobre pro testovani optimalizace vykreslovacich rutin//
   //fillPaint=new Color[]{new Color((float)Math.random(),(float)Math.random(),(float)Math.random() )};
   int iFill = 0, iDraw = 0;
   if(a instanceof Dilek){
     int por = ((Dilek)a).poradi;
     iFill = por % fillPaint.length;
     iDraw = por % drawColor.length;
   }
    if (fillPaint[iFill] != null) {
      g.setPaint(fillPaint[iFill]);
      g.fill(a);
    }
    if (drawStroke != null && drawColor[iDraw] != null) {
      g.setStroke(drawStroke);
      g.setColor(drawColor[iDraw]);
      //g.setPaint(drawPaint[iDraw]);
      g.draw(a);
      //g.fill(drawStroke.createStrokedShape(a));
    }
    //zvyazneni bodu pro kontrolu prevodu na obrys
    /*double[] d = new double[6];
     PathIterator p = a.getPathIterator(null);
     while (!p.isDone()) {
      g.setPaint(fillPaint);
      if (p.currentSegment(d) == PathIterator.SEG_MOVETO) {
	g.fill(new Ellipse2D.Double(d[0] - 0.1, d[1] - 0.1, 0.2, 0.2));
//	g.fill(new Ellipse2D.Double(d[0] - 0.2, d[1] - 0.2, 0.4, 0.4));
      }
      else if (p.currentSegment(d) != PathIterator.SEG_CLOSE) {
	g.fill(new Ellipse2D.Double(d[0] - 0.1, d[1] - 0.1, 0.2, 0.2));
//	g.fill(new Ellipse2D.Double(d[0] - 0.05, d[1] - 0.05, 0.1, 0.1));
      }
      p.next();
     }*/

  }

  public static Color[] a(Color p){
    return new Color[]{p};
  }

  public static Vzhled getDefaultObys(){
   return new Vzhled(null,a(null),a(Color.lightGray));
 }

   public static Vzhled getDefaultNahledFilled(){
    return new Vzhled(null,a(null),a(Color.darkGray));
  }

  public static Vzhled getDefaultNahledEmpty(){
    return new Vzhled(tlustyStroke,a(Color.darkGray),a(null));
  }

  public static Vzhled getDefaultNahledDilek(){
      return new Vzhled(tenkyStroke,a(Color.white),a(Color.darkGray));
  }

  public static Vzhled getDefaultHelp(){
        return new Vzhled(tenkyStroke,a(Color.lightGray),a(Color.lightGray));
  }
  public static Vzhled getDefaultObrys(){
   return new Vzhled(tlustyStroke,a(Color.black),a(Color.orange));
 }

  public static Vzhled getDefaultObrysVyber(){
    return new Vzhled(tlustyStroke,a(Color.black),a(Color.red));
  }

  public static Vzhled getDefaultDilek(){
    return new Vzhled(tenkyStroke,a(Color.darkGray),a(Color.orange));
  }

  public static Vzhled getDefaultDilekVyber(){
    return new Vzhled(tenkyStroke,a(Color.darkGray),a(null));
  }
  public static Vzhled getHidden(){
    return new Vzhled(null,a(null),a(null));
  }
  public float getLineWidth(){
      if(drawStroke != null && drawStroke instanceof BasicStroke)
        return ((BasicStroke)drawStroke).getLineWidth();
      return .0f;
  }
  public Color getLineColor(){
    return drawColor[0];
  }
  public Color getFillColor(){
      if(fillPaint[0] != null && fillPaint[0] instanceof Color)
        return (Color)fillPaint[0];
      return null;
  }
  public void setLineWidth(float w){
     drawStroke = w <= .0f ? null : new BasicStroke(w, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
  }
  public void setLineColor(Color c){
    drawColor = a(c);
  }
  public void setFillColor(Color c){
    fillPaint = c.getAlpha() == 0 ? a(null) : a(c);
  }
}
